# @shuaige : 陈世玉
# @name :test3.py
# @time :2025/1/2 12:56
import json
import requests
import pandas as pd
from parsel import Selector
from bs4 import BeautifulSoup

# 搜索汽车名称url
get_car_id_url = "https://www.dongchedi.com/search?keyword={car_name}&currTab=1&city_name={city_name}&search_mode=history"

# headers必须要有
headers = {
    'pragma': 'no-cache',
    'accept-language': 'zh-CN,zh;q=0.9',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36',
    'accept': '*/*',
    'cache-control': 'no-cache',
    'authority': 'www.dongchedi.com'
}


# 获取车辆id的函数，最后返回id
def get_car_id(car_name, city_name):
    carid_url = get_car_id_url.format(car_name=car_name, city_name=city_name)
    response = requests.get(url=carid_url, headers=headers).text
    selector = Selector(text=response)
    car_message = selector.css('''.dcd-car-series a::attr(data-log-click)''').get()
    car_message = json.loads(car_message)
    car_id = car_message.get("car_series_id")
    return car_id


# 获取车辆详情
def get_car_detail(car_id):
    # 汽车详情页url
    url = "https://www.dongchedi.com/auto/params-carIds-x-" + str(car_id)
    # 发送页面请求
    response = requests.get(url, headers)
    # 判定是否请求成功
    if response.status_code == 200:
        print('请求成功(*^▽^*)')
    else:
        print('请求网页失败┭┮﹏┭┮')
    # 获取需要进一步加工的网页数据
    html = requests.get(url, headers=headers)
    # 设置系统默认编码为UTF-8，防止乱码
    html.encoding = 'utf-8'
    # 使用BeautifulSoup解析这段代码，得到一个BeautifulSoup对象
    soup = BeautifulSoup(html.text, 'lxml')
    # 按照标准的缩进格式的结构输出
    # print(soup.prettify())
    trs = soup.find_all('div', class_='table_row__yVX1h')[3:12]
    car_info = []
    for tr in trs:
        infos = list(tr.stripped_strings)
        car_info.append(infos)
    # print(car_info)
    return car_info


# 启动函数
def main(car_name, city_name):
    car_id = get_car_id(car_name=car_name, city_name=city_name)
    car_info = get_car_detail(car_id=car_id)
    df = pd.DataFrame(car_info)
    # print(df)
    writer = pd.ExcelWriter(r"result.xlsx", mode='a', if_sheet_exists='new')  # mode如果不写则只会保留最后一次数据
    df.to_excel(writer, sheet_name=car_name, index=False)
    writer.close()  # 升级了Python，writer.save()会报错


if __name__ == '__main__':
    car_list = ["海豚", "荣放", "普拉多", "兰德酷路泽"]  # , "欧萌达", "瑞虎5X"
    for i in car_list:
        main(i, "重庆")